/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/*
测试用例
1->2->3->4->5
 */

let list = {
    val: 1,
    next: {
        val: 2,
        next: {
            val: 3,
            next: {
                val: 4,
                next: {
                    val: 5,
                    next: null
                }
            }
        }
    }
}




/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function (head) {
    let tmp = head
    if(head.next&&head){
        while(head.next){
            head = head.next
        }
        let reverse = function (node, next) {
            if (next.next) reverse(node.next, next.next)
            next.next = node
        }
        reverse(tmp, tmp.next)
    }
    return head
};


reverseList = function(head) {
    let stack = []
    if(head){
        while(head){
            stack.push(head)
            head = head.next
        }
        let f = stack[stack.length-1]
        while (stack.length>1) {
            let tmp = stack.pop()
            // stack[stack.length-1].next = null
            tmp.next = stack[stack.length-1]
        }
        return f
    }
    return head
};

reverseList = function(head){
    let n = null
    let nP = head
    while(nP!=null){
        let tmp = nP.next
        nP.next = n
        n = nP
        nP = tmp
    }
    return n
}
console.log(reverseList(list));